package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.text.TextUtils.TruncateAt;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.activity.BaseActivity;
import com.jdy.haoduoai.enums.MediaState;
import com.jdy.haoduoai.enums.SelectionMode;
import com.jdy.haoduoai.model.LeHotDynamicItem;
import com.jdy.haoduoai.util.FileUtils;
import com.jdy.haoduoai.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * 我的配音
 * 
 * @author Hufei
 * 
 */
@SuppressLint("InflateParams")
public class MyVoiceAdapter extends BaseAdapter implements OnClickListener {

  private BaseActivity mContext;
  private LayoutInflater mInflater;
  private ArrayList<LeHotDynamicItem> mList;
  private DisplayImageOptions options;
  private MediaState mMediaState = MediaState.NotDownload;
  private String video_url, mp3_url;
  private FileUtils fileutils;
  private SparseArray<View> cvArray = new SparseArray<>();
  private boolean isEditor = false;

  public SelectionMode SelectMode = SelectionMode.NONE;

  public void remove(LeHotDynamicItem object) {
    mList.remove(object);
    notifyDataSetChanged();
  }

  public void setEditor(boolean iseditor) {
    this.isEditor = iseditor;
  }

  @SuppressWarnings("static-access")
  public void setSelectMode(SelectionMode sm) {
    SelectMode = sm;

    if (mList != null && !mList.isEmpty()) {
      if (SelectMode == SelectMode.SELECT_ALL) {
        for (LeHotDynamicItem item : mList) {
          item.isChecked = true;
        }
      } else if (SelectMode == SelectMode.DESELECT_ALL) {
        for (LeHotDynamicItem item : mList) {
          item.isChecked = false;
        }
      }
    }
  }

  public void removeSelection() {
    for (LeHotDynamicItem item : mList) {
      item.isChecked = false;
    }

    notifyDataSetChanged();
  }

  public ArrayList<LeHotDynamicItem> getList() {
    return mList;
  }

  public MyVoiceAdapter(BaseActivity context, ArrayList<LeHotDynamicItem> list,
                        String sno) {
    this.mContext = context;
    this.mList = list;
    mInflater = LayoutInflater.from(mContext);

    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem).cacheInMemory(true)
        .bitmapConfig(Bitmap.Config.RGB_565).build();
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs().build();
    ImageLoader.getInstance().init(configuration);
    fileutils = new FileUtils();

  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    if (position >= getCount())
      return null;
    else
      return mList.get(position);

  }

  @Override
  public long getItemId(int arg0) {
    return arg0;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LeHotDynamicItem item = mList.get(position);

    convertView = cvArray.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.myvoice_item_layout, null);

      holder.shareImg = (ImageView) convertView.findViewById(R.id.shareimg);
      holder.title = (TextView) convertView.findViewById(R.id.titname);
      holder.uploadtime = (TextView) convertView.findViewById(R.id.uploadtime);
      holder.radiobtn = (CheckBox) convertView.findViewById(R.id.radiobtn);

      holder.top_v = (RelativeLayout) convertView.findViewById(R.id.top_v);
      holder.playtimes = (TextView) convertView.findViewById(R.id.playtimes);
      holder.liketimes = (TextView) convertView.findViewById(R.id.liketimes);

      convertView.setTag(holder);
      cvArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    if (isEditor) {
      holder.radiobtn.setVisibility(View.VISIBLE);
      holder.uploadtime.setVisibility(View.GONE);
      holder.title.setMaxEms(10);
      holder.title.setEllipsize(TruncateAt.END);
    } else {
      holder.radiobtn.setVisibility(View.GONE);
      holder.uploadtime.setVisibility(View.VISIBLE);
      holder.title.setMaxEms(5);
      holder.title.setEllipsize(TruncateAt.END);
    }

    holder.top_v.setTag(position);
    holder.top_v.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        int pos = (int) v.getTag();
        if (isEditor) {
          holder.radiobtn.setChecked(!holder.radiobtn.isChecked());
          mList.get(pos).isChecked = holder.radiobtn.isChecked();
        }
      }
    });

    holder.radiobtn.setChecked(mList.get(position).isChecked);

    if (Tools.isNotEmpty(item)) {
      holder.uploadtime.setText(item.updated_at.split(" ")[0]);
      holder.title.setText(item.dub_name);
      holder.playtimes.setText("" + item.play_count);
      holder.liketimes.setText("" + item.like);
      ImageLoader.getInstance().displayImage(item.dub_cover, holder.shareImg,
          options);
    }

    return convertView;
  }

  private static class ViewHolder {
    private ImageView shareImg;
    private TextView title;
    private RelativeLayout top_v;
    private TextView uploadtime;
    private TextView playtimes;
    private TextView liketimes;
    private CheckBox radiobtn;
  }

  @Override
  public void onClick(View v) {

  }

  // public void checkFile() {
  // try {
  // String[] mp3 = URLDecoder.decode(mp3_url, "UTF-8").split("/");
  // String[] mp4 = URLDecoder.decode(video_url, "UTF-8").split("/");
  // System.out.println("=====mp3===" + mp3[mp3.length - 1] + ";;mp4=="
  // + mp4[mp4.length - 1]);
  // if (fileutils.isFileExist("xiaoxiaoban/share/" + mp3[mp3.length - 1])
  // && fileutils.isFileExist("xiaoxiaoban/share/" + mp4[mp4.length - 1])) {
  // if (mMediaState == MediaState.Pause) {
  // mMediaState = MediaState.Pause;
  // } else {
  // mMediaState = MediaState.Downloaded;
  // }
  // }
  // } catch (UnsupportedEncodingException e) {
  // e.printStackTrace();
  // }
  // }

  // public String getName(String url) {
  // String[] out = null;
  // try {
  // out = URLDecoder.decode(url, "UTF-8").split("/");
  // } catch (UnsupportedEncodingException e) {
  // e.printStackTrace();
  // }
  // String path = Environment.getExternalStorageDirectory()
  // + "/haoduoai/share/" + out[out.length - 1];
  // return path;
  // }

}
